package club.pchome.utils;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileReader;
import java.io.IOException;

public class ExecuJs {

    public static String execuJs(String url){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        String c = null;
        try {
            String jsFileName = "e:\\home\\0805.js";
            FileReader reader = new FileReader(jsFileName);
            // 执行指定脚本
            engine.eval(reader);

            if(engine instanceof Invocable) {
                Invocable invoke = (Invocable)engine;
                // 调用merge方法，并传入两个参数
                c = (String) invoke.invokeFunction("get_anti", url);
            }
            reader.close();
        } catch (NoSuchMethodException | ScriptException | IOException e) {
            e.printStackTrace();
        }
        return c;
    }

    public static String encryptPasswd(String passwd){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        String c = null;
        try {
            String jsFileName = "e:\\home\\pinduoduo.js";
            FileReader reader = new FileReader(jsFileName);
            // 执行指定脚本
            engine.eval(reader);

            if(engine instanceof Invocable) {
                Invocable invoke = (Invocable)engine;
                // 调用merge方法，并传入两个参数
                c = (String) invoke.invokeFunction("K", passwd);
            }
            reader.close();
        } catch (NoSuchMethodException | ScriptException | IOException e) {
            e.printStackTrace();
        }
        return c;
    }
}